from fastapi import HTTPException, status
from sqlalchemy.orm import Session
from ..sql import schema, models
from ..auth import crud
from .. import exceptions



def get_all_stations(db: Session):
    return db.query(models.Station).all()

def is_category_exist(db: Session, name: str):
    return db.query(models.Category).filter(models.Category.name==name).first()

def is_category_exist_by_id(db: Session, cid: int):
    return db.query(models.Category).filter(models.Category.id==cid).first()

def add_category(db: Session, name: str):
    if is_category_exist(db, name):
        raise exceptions.category_already_exist_exception
    else:
        db_category = models.Category(name=name)
        db.add(db_category)
        db.commit()
        return "ok"

def get_categories(db: Session):
    return db.query(models.Category).all()

def add_transcations(db: Session, uid: int, name: str, cost: float, category_id: int):
    if is_category_exist_by_id(db, category_id):
        db_transcation = models.Transaction(name=name, cost=cost, category_id=category_id, owner_id=uid)
        db.add(db_transcation)
        db.commit()
        return "ok"
    else:
        raise exceptions.category_dont_exist_exception

def get_transactions(db: Session, uid: int):
    return crud.get_user_by_id(db, uid).transactions



